#include "../server.hpp"

class EchoServer
{
    using PtrConnection = std::shared_ptr<Connection>;
private:
    TcpServer _server;
private:
    void OnConnected(const PtrConnection& conn)
    {
        std::cout << "new client link! con_id: " << conn->GetId() << std::endl;
    }

    void OnClosed(const PtrConnection& conn)
    {
        std::cout << "close client link! con_id: " << conn->GetId() << std::endl;  
    }

    void OnMessage(const PtrConnection& conn, Buffer* buf)
    {
        std::cout << "Server# Client say: " << buf->ReadPosition() << std::endl;
        conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        conn->ShutDown();
    }
public:
    EchoServer(const uint32_t port) :_server(port)
    {
        _server.SetThreadCount(10);
        _server.EnableInactiveRelease(10);
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
    }
    void Start()
    {
        _server.Start();
    }
};
